<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>检查某个名称的常量是否存在</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.define.html">« define</a></li>
      <li style="float: right;"><a href="function.die.html">die »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.misc.html">杂项 函数</a></li>
    <li>检查某个名称的常量是否存在</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.defined" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">defined</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">defined</span> &mdash; <span class="dc-title">检查某个名称的常量是否存在</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.defined-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>defined</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$constant_name</code></span>): <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   检查该名称的常量是否已定义。
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    如果你要检查一个变量是否存在，请使用 <span class="function"><a href="function.isset.html" class="function">isset()</a></span>。
    <span class="function"><strong>defined()</strong></span> 函数仅对 <a href="language.constants.html" class="link">常量</a>
    有效。如果你要检测某个函数是否存在，使用 <span class="function"><a href="function.function-exists.html" class="function">function_exists()</a></span>。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.defined-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">constant_name</code></dt>

     <dd>

      <p class="para">
       常量的名称。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.defined-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
  如果名称 <code class="parameter">constant_name</code> 的常量已定义，返回 <strong><code>true</code></strong>；未定义则返回 <strong><code>false</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.defined-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3445">
    <p><strong>示例 #1 检查常量</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;注意引号的使用，这很重要。&nbsp;&nbsp;&nbsp;这个例子是检查<br />&nbsp;*&nbsp;如果字符串&nbsp;'TEST'&nbsp;是&nbsp;TEST&nbsp;常量的名称&nbsp;*/<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">defined</span><span style="color: #007700">(</span><span style="color: #DD0000">'TEST'</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">TEST</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.defined-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.define.html" class="function" rel="rdfs-seeAlso">define()</a> - 定义一个常量</span></li>
    <li class="member"><span class="function"><a href="function.constant.html" class="function" rel="rdfs-seeAlso">constant()</a> - 返回一个常量的值</span></li>
    <li class="member"><span class="function"><a href="function.get-defined-constants.html" class="function" rel="rdfs-seeAlso">get_defined_constants()</a> - 返回所有常量的关联数组，键是常量名，值是常量值</span></li>
    <li class="member"><span class="function"><a href="function.function-exists.html" class="function" rel="rdfs-seeAlso">function_exists()</a> - 如果给定的函数已经被定义就返回 true</span></li>
    <li class="member">关于 <a href="language.constants.html" class="link">常量</a> 的章节</li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>